package AccionBoton;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ResourceBundle;

import javax.swing.JFileChooser;

import modelUtil.Configuracion;
import modelUtil.Util;
import paginas.PaginaEntornoCreacion;

public class AccionMoverUsuario implements ActionListener{

	private String _accion;
	private JFileChooser fc;
	private JFileChooser fc2;
	private PaginaEntornoCreacion _panel;
	
	public AccionMoverUsuario(PaginaEntornoCreacion panel,String accion){
		
		_accion = accion;
		_panel = panel;
		
	}
	
	public void actionPerformed(ActionEvent evt){

		File file = null;
		Object[] usuarios = _panel.getList().getSelectedValues();

		
		if(_accion.equals("exportar")){

		if(usuarios.length==0){
			
			Util.popup(0, ResourceBundle.getBundle("internacionalizacion/Etiquetas").getString("etiqueta_menu_duplicar_error_no_usuario"));
		}else{
        //Set up the file chooser.
			if (fc == null) {
				fc = new JFileChooser();
				fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			}
	        int returnVal = fc.showDialog(_panel,ResourceBundle.getBundle("internacionalizacion/Etiquetas").getString("etiqueta_menu_mover_usuario_abrir"));

	        //Process the results.
	        if (returnVal == JFileChooser.APPROVE_OPTION) {
	            file = fc.getSelectedFile();

	        } else {
	          
	        }
	        fc.setSelectedFile(null);

	       
		}
		}else{
			if (fc == null) {
	            fc = new JFileChooser();
	            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
	        }
			
	        int returnVal = fc.showDialog(_panel,ResourceBundle.getBundle("internacionalizacion/Etiquetas").getString("etiqueta_menu_mover_usuario_abrir"));

	        //Process the results.
	        if (returnVal == JFileChooser.APPROVE_OPTION) {
	            file = fc.getSelectedFile();

	        } else {
	          
	        }
	        fc.setSelectedFile(null);

			
		}
        //Show it.


        //Reset the file chooser for the next time it's shown.
    		
		if(_accion.equals("importar")){
			
			if(file!=null){
				try{
				Util.copyFiles(file, new File(Configuracion.instancia().getDirUsuarios()+"/"+file.getName()), true);
				_panel.getListModel().addElement(file.getName());
				_panel.repaint();
				}catch(Exception e){
					
					
				}
				
			}
			
		}
		
		if(_accion.equals("exportar")){

			
			if(file!=null){
				try{
					for(int i=0;i<usuarios.length;i++){
				Util.copyFiles(new File(Configuracion.instancia().getDirUsuarios()+"/"+usuarios[i].toString()), file, true);
				_panel.getListModel().addElement(file.getName());
					}
				_panel.repaint();
				}catch(Exception e){
					
					
				}
				
			}
			}
		
		
	}
	
}
